﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.Net;
using com.lover.common;
using System.IO;
using com.lover.astd.common.model;
using com.lover.common.http;
using com.lover.astd.common.manager;

namespace com.lover.astd.common.partner.impl
{
    public class CgaLogin : LoginImplBase
    {
        public CgaLogin() : base() { }
        public override bool alwaysNeedVerifyCode() { return true; }
        public override string getAlwaysCaptchaImageUrl() { return "http://passport.cga.com.cn/login/VCode.ashx"; }
        public override LoginResult login(ref List<Cookie> cookies, string verify_code = null, string extra = null)
        {
            LoginResult result = new LoginResult();
            logging();
            if (verify_code == null)
            {
                buildAlwaysVerifyResult(ref result, ref cookies);
                return result;
            }

            string tokenurl = "http://passport.cga.com.cn/login/Service/LoginService.ashx?op=CheckUserLogin&jsoncallback=jsonp" + TimeMgr.TimeStamp;
            HttpResult resp = TransferMgr.doGetPure(tokenurl, ref cookies);
            if (resp == null) { result.StatusCode = LoginStatusCode.FailInGetToken; return result; }

            string loginurl = "http://passport.cga.com.cn/login/LoginPost.aspx";
            string data = String.Format(
                "returnurl=http%3A%2F%2Fastd.cga.com.cn%2Fgame.html%3Fgameinfo%3D77%2C{0}&loginname={1}&password={2}&checkcode={3}&select={0}&x=75&y=23",
                _acc.ServerId, _username, _password, verify_code);
            resp = TransferMgr.doPostPure(loginurl, data, ref cookies, "http://astd.cga.com.cn/");
            if (resp == null) { result.StatusCode = LoginStatusCode.FailInLogin; return result; }
            string content = resp.getContent();
            if (resp.StatusCode != 302)
            {
                result.StatusCode = LoginStatusCode.FailInLogin;
                return result;
            }

            string redirecturl = String.Format("http://passport.cga.com.cn/login/sp/togame.aspx?gameinfo=77,{0}", _acc.ServerId);

            processRedirect(redirecturl, result, ref cookies);
            return result;
        }
    }
}
